import { resolve } from 'path';
import { AliasOptions, ServerOptions, defineConfig, loadEnv } from 'vite';
import { VitePluginNode } from 'vite-plugin-node';
export default defineConfig(({ mode }) => {
  const root = process.cwd();
  const { VITE_PORT } = loadEnv(mode, root);
  const resolvePath = (path: string) => resolve(root, '.', path);

  const server: ServerOptions = {
    port: Number(VITE_PORT),
    hmr: true,
  };
  const alias: AliasOptions = [
    {
      find: '@app/',
      replacement: resolvePath('src') + '/',
    },
  ];

  return {
    server,
    plugins: [
      ...VitePluginNode({
        // NodeJs 原生请求适配器
        // 支持'express', 'nest', 'koa' 和 'fastify',
        adapter: 'nest',

        // 项目入口文件
        appPath: './src/main',

        //
        appName: 'AppShopServer',

        // 在项目入口文件中导出的名字
        exportName: 'app',

        // 编译方式: esbuild 和 swc,
        // 默认 esbuild. 但esbuild 不支持 'emitDecoratorMetadata'
        // 使用swc需要安装 `@swc/core`
        tsCompiler: 'swc',
      }),
    ],
    resolve: {
      alias,
    },
    optimizeDeps: {
      exclude: ['@nestjs/microservices', '@nestjs/websockets', 'cache-manager', 'class-transformer', 'class-validator', 'fastify-swagger'],
    },
  };
});
